package cn.da.shuai.cool.finance.controller;

import cn.da.shuai.cool.common.entity.R;
import cn.da.shuai.cool.finance.model.CoolCostAccounting;
import cn.da.shuai.cool.finance.model.vo.SearchCostAccountingVo;
import cn.da.shuai.cool.finance.service.ICoolCostAccountingService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 成本核算表 前端控制器
 * </p>
 *
 * @author 大帅哥
 * @since 2023-05-01
 */
@Tag(name = "成本核算管理")
@RestController
@RequestMapping("/finance/cost-accounting")
@RequiredArgsConstructor
public class CoolCostAccountingController {

    private final ICoolCostAccountingService costAccountingService;

    @Operation(summary = "分页查询成本核算")
    @PostMapping("/page")
    public R<Page<CoolCostAccounting>> page(@RequestBody SearchCostAccountingVo vo) {
        LambdaQueryWrapper<CoolCostAccounting> wrapper = new LambdaQueryWrapper<CoolCostAccounting>()
                .eq(vo.getCostObjectId() != null, CoolCostAccounting::getCostObjectId, vo.getCostObjectId())
                .eq(vo.getCostObjectType() != null, CoolCostAccounting::getCostObjectType, vo.getCostObjectType())
                .eq(vo.getCostType() != null, CoolCostAccounting::getCostType, vo.getCostType())
                .eq(vo.getAccountingPeriod() != null, CoolCostAccounting::getAccountingPeriod, vo.getAccountingPeriod())
                .orderByDesc(CoolCostAccounting::getAccountingPeriod);

        return R.ok(costAccountingService.page(new Page<>(vo.getPageNum(), vo.getPageSize()), wrapper));
    }

    @Operation(summary = "获取成本核算详情")
    @GetMapping("/{id}")
    public R<CoolCostAccounting> getById(@PathVariable Long id) {
        return R.ok(costAccountingService.getById(id));
    }

    @Operation(summary = "新增成本核算")
    @PostMapping
    public R<Boolean> add(@RequestBody CoolCostAccounting costAccounting) {
        return R.ok(costAccountingService.save(costAccounting));
    }

    @Operation(summary = "修改成本核算")
    @PutMapping
    public R<Boolean> update(@RequestBody CoolCostAccounting costAccounting) {
        return R.ok(costAccountingService.updateById(costAccounting));
    }

    @Operation(summary = "删除成本核算")
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable Long id) {
        return R.ok(costAccountingService.removeById(id));
    }
}